﻿//选择客户(单选）
var columns =
            [
                 { display: 'ID', name: 'ID', type: 'text', hide: 1 },
                 { display: '产品类别', name: 'TypeName', mintWidth: 80, width: 120 }, 
                { display: '产品名称', name: 'ProductName', mintWidth: 80, width: 120 }, 
                {display: '产品编码', name: 'ProductCode', mintWidth: 100, width: 140 },
                { display: '产品规格', name: 'ProductStandard', mintWidth: 100, width: 140 },
                { display: '所属仓库', name: 'StorehouseName', mintWidth: 80, width: 120 }
             ];

var manager;
$(document).ready(function () {
    initligerGrid();
    $("#btnSearch").unbind("click").bind("click", initligerGrid);
    $("#submitBtn").unbind("click").bind("click", selectOperation);
});

function initligerGrid() {
    var wh = $(window).height();
    //var fh = $("#" + formDiv).height();
    var gridHeight = 438; //  wh - fh - 50;
    manager = null;
    manager = $("#productsGrid").ligerGrid({
        columns: columns,
        title: '产品信息列表',
        showTitle: true,
        width: '100%',
        enabledSort: true,
        height: gridHeight,
        checkbox: true,
        rownumbers: true,
         isChecked: f_isChecked, onCheckRow: f_onCheckRow, onCheckAllRow: f_onCheckAllRow,
        parms: { orgCode: $("#HidOrgCode").val(), productGG: $("#productGG").val(), TypeName: $("#TypeName").val(),productName: $("#productName").val(), productCode: $("#productCode").val() },
        url: "../Product/GetJsonOfProducts?orgCode=" + $("#HidOrgCode").val(),
    });
}


//update 
function selectOperation() {
//    var row = manager.getSelectedRow();
//    if (!row) {
//        $.ligerDialog.success('请选择产品!'); return;
    //    }

//    var row = manager.getSelectedRow();
//    if (!row) { alert('请选择行'); return; }

        var strHtml=new Array([checkedCustomer.length]);
        var data = manager.rows;        var len=data.length;               for(var i=0;i<len;i++)       {            for(var j=0;j<checkedCustomer.length;j++)            {                if(data[i]["ID"]==checkedCustomer[j])                {                    strHtml[j]=checkedCustomer[j]+","+data[i]["ProductName"]+ "," + data[i]["TypeName"]+ "," + data[i]["ProductCode"] + "," + data[i]["ProductStandard"];                }            }       }      // strHtml+="]";
   // alert(strHtml);    window.returnValue = strHtml;
    window.close();
}

//=========================================
 function f_onCheckAllRow(checked)
        {
            for (var rowid in this.records)
            {
                if(checked)
                    addCheckedCustomer(this.records[rowid]['ID']);
                else
                    removeCheckedCustomer(this.records[rowid]['ID']);
            }
        }

        /*
        该例子实现 表单分页多选
        即利用onCheckRow将选中的行记忆下来，并利用isChecked将记忆下来的行初始化选中
        */
        var checkedCustomer = [];
        function findCheckedCustomer(CustomerID)
        {
            for(var i =0;i<checkedCustomer.length;i++)
            {
                if(checkedCustomer[i] == CustomerID) return i;
            }
            return -1;
        }
        function addCheckedCustomer(CustomerID)
        {
            if(findCheckedCustomer(CustomerID) == -1)
                checkedCustomer.push(CustomerID);
        }
        function removeCheckedCustomer(CustomerID)
        {
            var i = findCheckedCustomer(CustomerID);
            if(i==-1) return;
            checkedCustomer.splice(i,1);
        }
        function f_isChecked(rowdata)
        {
            if (findCheckedCustomer(rowdata.ID) == -1)
                return false;
            return true;
        }
        function f_onCheckRow(checked, data)
        {
            if (checked) addCheckedCustomer(data.ID);
            else removeCheckedCustomer(data.ID);
        }
        function f_getChecked()
        {
            alert(checkedCustomer.join(','));
        }